//
//  ViewController.m
//  salesTerminals_2
//
//  Created by xx xx on 12-8-26.
//  Copyright (c) 2012年 xx xx. All rights reserved.
//


#import "ASIHTTPRequest.h"
#import "JSONKit.h"
#import "MessageController.h"
#import "ASIHTTPRequest.h"
//
//  simMessageViewController.m
//  simens
//
//  Created by xx xx on 12-7-17.
//  Copyright (c) 2012年 yst. All rights reserved.
//



@implementation Message

@synthesize mid,mobile,smscontent,smstype,sendtime,result,state,title;


@end


@interface MessageController () /*<ASIHTTPRequestDelegate>*/
@property (nonatomic,strong) UITableView *messageTableView;


@end

@implementation MessageController
@synthesize dataList;
@synthesize messageTableView = _messageTableView;



NSMutableArray* messageItemArray;
NSMutableArray* sendMessageItemArray;
NSArray* reportArray;
NSArray* tableViewArray;
//ASIHTTPRequest* initRequest;
//ASIHTTPRequest* countRequest;
bool vistType;
NSString* name;
ASIHTTPRequest* initRequest;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    
    
    self.title=@"我的中心";
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.messageTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,
                                                                          self.view.bounds.size.height)
                                                         style:UITableViewStylePlain];
    
    [self.messageTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.messageTableView setDelegate:self];
    [self.messageTableView setDataSource:self];
    [self.view addSubview: self.messageTableView];
    
    
    /*
    messageItemArray = [[NSMutableArray alloc] init];
    
    for (int i = 1; i < 10; i++) {
        Message* message = [[Message alloc] init];
        
        //message.mid = [NSString stringWithFormat:@"%d",[temp objectForKey:@"id"]];
        message.mid = [NSString stringWithFormat:@"%d",i];
        message.sendtime = [@"2012-08-0" stringByAppendingString:[NSString stringWithFormat:@"%d", i]];
        message.smscontent =[NSString stringWithFormat:@"%@",@"通知"];
        message.mobile = @"13666666666" ;
        message.smstype = @"TZ";
        message.state = @"Y";
        message.title = [NSString stringWithFormat:@"%@",@"标题" ];
        [messageItemArray addObject:message];
    }
    
    self.dataList = messageItemArray;
    */
    /*
    UIBarButtonItem* tl = [[UIBarButtonItem alloc] initWithTitle:@"发送指令" style:UIBarButtonItemStylePlain target:self action:@selector(sendCommand)];
    self.navigationItem.leftBarButtonItem = tl;
    */
    [super viewDidLoad];
    
    
	// Do any additional setup after loading the view.
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self showMbprogress];
    
    /*
    NSString *u =[NSString stringWithFormat:@"%@/getSendWaitMess?mobile=%@&pwd=%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"WebserviceUrl"],[[NSUserDefaults standardUserDefaults]objectForKey:@"LoginMobile"],[[NSUserDefaults standardUserDefaults]objectForKey:@"LoginPassword"]];
    */
    NSString *u = @"http://cmsdev.nfsq.com.cn:8080/smsPlatform/smsPlatformCXF/rs/sms/getSendWaitMessage?mobile=15029085805";
    NSURL *url = [NSURL URLWithString: u];
    
    initRequest = [ASIHTTPRequest requestWithURL:url];
    [initRequest setDelegate:self];
    [initRequest start];

}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
        return 1;
   
}// Default is 1 if not implemented



//tableview行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
        
            return [messageItemArray count];
       
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    
    NSString* CellIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.section, indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    
    for (UIView *v in [cell subviews]) {
        if ([v isKindOfClass:[UILabel class]] |
            [v isKindOfClass:[UIImage class]] |
            [v isKindOfClass:[UIImageView class]]|
            [v isKindOfClass:[UIProgressView class]]) {
            [v removeFromSuperview];
        }
    }
    
    
    [cell.textLabel setBackgroundColor:[UIColor clearColor]];
    
    
    CGFloat x1;
    CGFloat x2;
    if(vistType) {
        x1 = 10;
        x2 = 110;
    } else{
        x1 = 10;
        x2 = 110;
    }
    
    
        
                   
            Message* message;
            
            if ([messageItemArray count] != 0) {
                message = [messageItemArray objectAtIndex:indexPath.row];
            }
            
            NSString *sendtime = message.sendtime;
            NSString *date = [sendtime substringToIndex:10];
            
            UILabel* sendDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(x1, 37, 100, 40)];
            [sendDateLabel setBackgroundColor:[UIColor clearColor]];
            [sendDateLabel setText:date];
            sendDateLabel.font=[UIFont systemFontOfSize:18];
            //sendDateLabel.textAlignment = UITextAlignmentCenter;
            [cell addSubview:sendDateLabel];
            
                       
            //message.title
            NSArray * contents =[message.smscontent componentsSeparatedByString:@";"];
            NSString *temp = [[NSString alloc] init];
            int jj = 0 ;
        for (int i=0;i<contents.count; i++) {
                temp = [NSString stringWithFormat:@"%dhyLabel",i];
                
                if(i==0){
                    jj=0;
                }else{
                    jj= jj+15;
                }
            
                NSString *content = [contents objectAtIndex:i] ;
                int clength = content.length;
            
                int endLength = clength-4;
            NSString *tempContent = [[NSString alloc] init];
            
            if(endLength>0){
                tempContent = [content substringWithRange:NSMakeRange(0,endLength)];
            }
            
                /*
            NSLog(@"%@=======",tempContent);
                      */  
                UILabel* temp = [[UILabel alloc] initWithFrame:CGRectMake(x2, jj, 210, 30)];
                [temp setBackgroundColor:[UIColor clearColor]];
                [temp setText:[NSString stringWithFormat:@"%@", tempContent]];
                [temp setTextColor:[UIColor blueColor]];
                temp.font=[UIFont systemFontOfSize:12];
                [cell addSubview:temp];
                
            }
        
        
    UIImage* seletedImg = [UIImage imageNamed:@"tableviewcellSelected.png"];
    UIImageView *seletedImgView =[[UIImageView alloc] initWithImage:seletedImg];
    [cell setSelectedBackgroundView:seletedImgView];
    
    UIImage* backImg = [UIImage imageNamed:@"tableviewcell.png"];
    UIImageView *backImgView =[[UIImageView alloc] initWithImage:backImg];
    [cell setBackgroundView:backImgView];
    /**/
    return cell;
    
}

//row高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
      
    
    return 108;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 30;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        
            NSUInteger row = [indexPath row];
            Message* message = [messageItemArray objectAtIndex:row];
    
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    
        return @"工厂发货消息：";
    
    
}



- (void)requestFinished:(ASIHTTPRequest *)request {
    NSError *error = [request error];
    if(request==initRequest){
        if (!error) {
            [self showMbprogress];
            NSString *response = [request responseString];
            NSLog(@"%@@=========",response);
            NSDictionary* dict = [response objectFromJSONString];
            reportArray = [dict objectForKey:@"sendWait"];
            tableViewArray = reportArray;
            messageItemArray = [[NSMutableArray alloc] init];
            if(reportArray != nil){
                for (int i = 0; i < reportArray.count; i++) {
                    Message* message = [[Message alloc] init];
                    NSDictionary* temp = [reportArray objectAtIndex:i];
                    
                    //message.mid = [NSString stringWithFormat:@"%d",[temp objectForKey:@"id"]];
                    message.mid = [[temp objectForKey:@"id"] stringValue];
                    message.sendtime = [temp objectForKey:@"send_time"];
                    message.smscontent =[NSString stringWithFormat:@"%@",[temp objectForKey:@"smscontent"]];
                    message.mobile = [[temp objectForKey:@"mobile"] stringValue];
                    message.smstype = [temp objectForKey:@"smstype"];
                    
                    [messageItemArray addObject:message];
                }
                
            }
            
            [self hideMbProgress];
            [self.messageTableView reloadData];
            
        }
        
        
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request {
    [self hideMbProgress];
}

/*
- (void)modifyPasswd {
    simModifyPasswdViewController * modifyPasswdController = [[simModifyPasswdViewController alloc] init];
    [self.navigationController pushViewController:modifyPasswdController animated:YES];
}
*/


@end
